define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'club_dtl/index' + location.search,
                    add_url: 'club_dtl/add',
                    edit_url: 'club_dtl/edit',
                    del_url: 'club_dtl/del',
                    multi_url: 'club_dtl/multi',
                    import_url: 'club_dtl/import',
                    table: 'club_dtl',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                search:false,
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        {
                            field: 'club.name',
                            title: __('Club'),
                            operate: 'LIKE',
                            halign: "center",
                            searchable: false,
                            formatter: function(value, row, index) {
                                return value || '';
                            }
                        },
                        {field: 'name', title: __('Name'), operate: 'LIKE', table: table, class: 'autocontent', formatter: Table.api.formatter.content},
                        // {field: 'stage', title: __('Stage'), operate: 'LIKE', table: table, class: 'autocontent', formatter: Table.api.formatter.content},
                        // {field: 'grade', title: __('Grade'), operate: 'LIKE', table: table, class: 'autocontent', formatter: Table.api.formatter.content},
                        {field: 'sort', title: __('Sort')},
                        {field: 'img_url', title: __('Img_url'), events: Table.api.events.image, formatter: Table.api.formatter.image, operate: false},
                        {
                            field: 'user.nickname',
                            title: __('User'),
                            operate: 'LIKE',
                            halign: "center",
                            searchable: false,
                            formatter: function(value, row, index) {
                                return value || '';
                            }
                        },
                        {field: 'remark', title: __('Remark'), operate: 'LIKE', table: table, class: 'autocontent', formatter: Table.api.formatter.content},
                        {field: 'addtime', title: __('Addtime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false},
                        {
                            field: 'operate',
                            title: __('Operate'),
                            table: table,
                            events: Table.api.events.operate,
                            formatter: function(value, row, index) {
                                var that = this;
                                var buttons = [];
                                // 默认操作按钮（编辑、删除）
                                buttons.push(Table.api.formatter.operate.call(this, value, row, index));
                                
                                // 专栏课程按钮
                                buttons.push('<a href="javascript:;" class="btn btn-xs btn-success btn-column-course" data-id="' + row.id + '" data-user-id="' + (row.user_id || '') + '" data-teacher-detail-id="' + (row.teacher_detail_id || '') + '" title="专栏课程">\
                                    <i class="fa fa-book"></i> 专栏课程\
                                </a>');
                                
                                return buttons.join(' ');
                            }
                        }
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
            
            // 绑定专栏课程按钮点击事件
            table.on('click', '.btn-column-course', function() {
                var user_id = $(this).data('user-id');
                var teacher_detail_id = $(this).data('teacher-detail-id');
                if (!user_id || user_id == 0) {
                    Layer.msg('该子社团还没有设置专栏教师，无法查看专栏课程');
                    return false;
                }
                // 跳转到专栏课程展示页面，传递user_id和teacher_detail_id参数
                var url = 'teacher_course_display/index?user_id=' + user_id;
                if (teacher_detail_id && teacher_detail_id != 0) {
                    url += '&teacher_detail_id=' + teacher_detail_id;
                }
                Fast.api.open(url, '专栏课程');
            });
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});
